// pages/Group_info/Group_info.js
var tools = require('../../js/tools.js')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        // group_name:'吴大瞎',
        // group_ID:'44445555',
        // name_in_group:'猪头吴',
        // zz:'是'
        array: [{
            group_number: 'XXXXXXXXXXXXX',
            name: 'XXX',
            NbrMenbr: 0,
            current_activity: 0
        }, {
            group_number: 'XXXXXXXXXXXXX',
            name: 'XXX',
            NbrMenbr: 0,
            current_activity: 0
        }],
        ifloading: false,
        thisnum: '',
        showmask: false,
        ifsaveloading: false,
        ifsavenameloading: false,
        value: '',//组昵称
        visible: false,
        originalname: '',
        actions2: [
            {
                name: '修改',
                color: '#ed3f14'
            }
        ]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // console.log(options)
        wx.getStorage({
            key: 'Landingtime',
            success(res) {
              // console.log("登录时间信息存在，正在检查是否过期..")
              // tools.gettimestamp()
              var date = tools.convertDateFromString(res.data)
              if (tools.comparetime(date) == 0) {
                wx.navigateBack({
                    delta: 1
                });
                //已经过期
                wx.showToast({
                  title: '请先点击"我的"里的头像登录',
                  icon: 'none',
                  duration: 3000
                })
              }
              else{
              }
            },
            fail(res) {//此时为第一次登陆
                wx.navigateBack({
                    delta: 1
                });
              wx.showToast({
                title: '请先点击"我的"里的头像登录',
                icon: 'none',
                duration: 3000
              })
            }
          })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var that = this
        wx.getStorage({
            key: 'pwdWithSalt',
            success(res) {
                wx.request({
                    url: 'https://localhost:8080/getGroupinfo/',
                    data: {
                        pwdWithSalt: res.data
                    },
                    success(res) {
                        that.setData({
                            array: res.data
                        })
                    }
                })
            }
        });


    },

    Becode: function (e) {
        var that = this
        this.setData({
            ifloading: true,
        })
        wx.getStorage({
            key: 'pwdWithSalt',
            success(res) {
                wx.request({
                    url: 'https://localhost:8080/ifadmin/',
                    data: {
                        pwdWithSalt: res.data,
                        groupnum: e.currentTarget.dataset.item
                    },
                    success(res) {
                        console.log(res.data)
                        if (res.data == 'True') {
                            that.setData({
                                ifloading: false
                            })
                            wx.navigateTo({
                                url: '../../pages/one_info/one_info?group_number=' + e.currentTarget.dataset.item
                            })
                        }
                        else if (res.data == 'False') {
                            that.setData({
                                ifloading: false,
                                thisnum: e.currentTarget.dataset.item,
                                showmask: true
                            })
                            console.log('加载图片')
                        }
                    }
                })
            }
        })

    },

    masktap: function (res) {
        this.setData({
            ifloading: false,
            thisnum: '',
            showmask: false,
            ifsaveloading: false,
            ifsavenameloading: false,
            value: '',
        })
    },

    Bsave: function (res) {
        var that = this
        this.setData({
            ifsaveloading: true
        })
        wx.downloadFile({
            url: 'http://qr.liantu.com/api.php?text=' + that.data.thisnum,     //仅为示例，并非真实的资源
            success: function (res) {
                // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
                if (res.statusCode === 200) {
                    wx.saveImageToPhotosAlbum({
                        filePath: res.tempFilePath,
                        success(res) {
                            wx.showToast({
                                title: '保存二维码成功！',
                            })
                            that.setData({
                                ifsaveloading: false
                            })
                        },
                        fail(res) {
                            wx.showToast({
                                title: '保存二维码失败！',
                                icon: 'none'
                            })
                            that.setData({
                                ifsaveloading: false
                            })
                        }
                    })
                }
            }
        })
    },

    Bsavename: function (res) {
        var that = this
        that.setData({
            ifsavenameloading: true,
        })
        if (this.data.value == '') {
            wx.showToast({
                title: '昵称不能为空!',
                icon: 'none'
            })
            that.setData({
                ifsavenameloading: false,
            })
        }
        else {
            console.log('ss')
            wx.getStorage({
                key: 'pwdWithSalt',
                success(res) {
                    wx.request({
                        url: 'https://localhost:8080/getnickname',
                        data: {
                            pwdWithSalt: res.data,
                            groupnum: that.data.thisnum
                        },
                        success(res) {
                            if (res.data == '0') {
                                that.setData({
                                    originalname: '无'
                                })
                            }
                            else {
                                that.setData({
                                    originalname: res.data
                                })
                            }

                        }
                    })
                }
            })

            that.setData({
                ifsavenameloading: false,
                visible: true
            })
        }
    },
    onChange: function (event) {
        //把输入的值存入变量
        this.setData({
            value: event.detail.detail.value
        })
    },

    handleClickItem: function () {
        var that = this
        const action = this.data.actions2
        //
        action[0].loading = true;
        this.setData({
            actions2: action
        })
        //
        wx.getStorage({
            key: 'pwdWithSalt',
            success(res) {
                wx.request({
                    url: 'https://localhost:8080/savenickname',
                    data: {
                        pwdWithSalt:res.data,
                        groupnum: that.data.thisnum,
                        nickname:that.data.value
                    },
                    success(res){
                        wx.showToast({
                            title:'修改成功!'
                        })
                        action[0].loading = false;
                        that.setData({
                            actions2: action,
                            visible:false
                        })
                    }
                })
            }
        })


    },
    handleCancel: function () {
        this.setData({
            visible: false
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})